﻿<%@ WebHandler Language="C#" Class="YPContainerStepAttributeHandler" %>

using System;
using System.Web;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Linq;
using BusinessRule.Business;
using System.Data;
using Newtonsoft.Json;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;
using System.Collections;
using BusinessRule.DTO;
using System.Web.SessionState;
using BusinessRule.IBusiness;
using BusinessRule.BusinessFactory;
using Camstar.WebClient.WebServicesProxy.InSiteWebServices;
using DatabaseAccess.DBUtility;

//Description:批次、工序、配置表属性维护处理程序
//Copyright (c) : 通力凯顿（北京）系统集成有限公司
//Writer:zhangrj
//create Date:2022-01-17

public class YPContainerStepAttributeHandler : IHttpHandler, IRequiresSessionState
{
    YPContainerBusiness ypContainerBusiness = new YPContainerBusiness();
    uLMSCommonIBusiness m_CommonObj = uMESBusinessFactory.instance.GetuLMSCommonObject();
    uLMSCheckIBusiness checkBusiness = uMESBusinessFactory.instance.GetuLMSCheckObject();
    ApsPlanDownSendBusiness apsPlanDownSend = new ApsPlanDownSendBusiness();
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            var action = context.Request.Form["action"].ToString();
            switch (action)
            {
                /*数据查询获取操作*/
                case "InitPageData"://初始化页面数据
                    InitPageData(context);
                    break;
                case "GetContainerList"://主查询批次列表
                    GetContainerList(context);
                    break;
                case "getContainerDetails"://获取批次详细列表
                    GetContainerDetails(context);
                    break;
                case "GetContainerAttr"://获取批次属性
                    GetContainerAttr(context);
                    break;
                case "GetStepAttrList"://获取工序列表数据
                    GetStepAttrList(context);
                    break;
                case "GetBomProductAttr"://获取配置表列表数据
                    GetBomProductAttr(context);
                    break;
                case "GetStepNoShipList"://获取工序不加工列表数据
                    GetStepNoShipList(context);
                    break;
                case "GetUserRoleList"://获取用户角色
                    GetUserRoleList(context);
                    break;
                case "GetCommonTerm"://获取常用术语
                    GetCommonTerm(context);
                    break;
                case "GetStepWorkReportList"://获取报工备注信息
                    GetStepWorkReportList(context);
                    break;
                /*数据保存操作*/
                case "SaveContainerAttrDatas"://保存批次属性
                    SaveContainerAttr(context);
                    break;
                case "SaveStepAttrDatas"://保存工序属性
                    SaveContainerStepAttr(context);
                    break;
                case "SaveAccessoriesAttrDatas"://保存配置表属性
                    SaveAccessoriesAttr(context);
                    break;
                case "AddStepNoShipDatas"://添加工序不加工数据
                    AddStepNoShipDatas(context);
                    break;
                case "RemoveStepNoShipDatas"://移除工序不加工数据
                    RemoveStepNoShipDatas(context);
                    break;
                case "SaveCommonTerm"://保存常用术语
                    SaveCommonTerm(context);
                    break;
                case "SaveStepWorkReportData"://保存报工备注信息
                    SaveStepWorkReportData(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception e)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = "出错了：" + e.Message, Result = false }));
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    /// <summary>
    /// 初始化页面数据
    /// </summary>
    /// <param name="context"></param>
    public void InitPageData(HttpContext context)
    {
        CurrentUserInfo userInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
        uMESProcessResult result = new uMESProcessResult() { Result = true };
        //获取工厂
        Dictionary<string, string> dic = new Dictionary<string, string>();
        DataTable dtFactory = m_CommonObj.GetFactory(dic);
        if (string.IsNullOrWhiteSpace(userInfo.FactoryID))
        {
            DataRow dr = dtFactory.NewRow();
            dr["FACTORYID"] = "";
            dr["FACTORYNAME"] = "请选择";
            dtFactory.Rows.InsertAt(dr, 0);
        }
        else
        {
            DataRow[] temp = dtFactory.Select("FACTORYID='" + userInfo.FactoryID + "'");
            if (temp.Length > 0)
            {
                dtFactory = temp.CopyToDataTable();
            }
            else
            {
                dtFactory.Rows.Clear();
            }
        }
        dtFactory.TableName = "dtFactory";
        result.DBDataSet.Tables.Add(dtFactory);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 获取用户角色列表
    /// </summary>
    /// <param name="context"></param>
    public void GetUserRoleList(HttpContext context)
    {
        CurrentUserInfo userInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
        //查询工序备注常用术语角色列表
        uMESProcessResult result = new uMESProcessResult() { Result = true };
        DataTable dtRole = ypContainerBusiness.GetRoleListByUserId(userInfo.EmployeeID.ToString());
        result.DBTable = dtRole;
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 获取常用术语
    /// </summary>
    /// <param name="context"></param>
    public void GetCommonTerm(HttpContext context)
    {
        string roleName = context.Request.Form["roleName"].ToString();
        DataTable dt = ypContainerBusiness.GetCommonTerm(roleName);
        context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = true, DBTable = dt }));
    }
    /// <summary>
    /// 保存常用术语
    /// </summary>
    /// <param name="context"></param>
    public void SaveCommonTerm(HttpContext context)
    {
        CurrentUserInfo userInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
        Dictionary<string, string> para = new Dictionary<string, string>();
        para.Add("roleName", context.Request.Form["roleName"].ToString());
        para.Add("termValue", context.Request.Form["termValue"].ToString());
        para.Add("CreateEmployeeId", userInfo.EmployeeID.ToString());
        para.Add("factorycommontermId", context.Request.Form["factorycommontermId"].ToString().Trim(','));
        para.Add("isDelete", context.Request.Form["isDelete"].ToString());
        uMESProcessResult result = ypContainerBusiness.SaveStepCommonTerm(para);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }


    /// <summary>
    /// 查询批次列表（分页）
    /// </summary>
    /// <param name="context"></param>
    public void GetContainerList(HttpContext context)
    {
        CurrentUserInfo userInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
        Dictionary<string, object> para = new Dictionary<string, object>();
        para.Add("ContainerName", context.Request.Form["containerName"].ToString());
        para.Add("ScanContainerName", context.Request.Form["scanContainerName"].ToString());
        if (!string.IsNullOrWhiteSpace(para["ScanContainerName"].ToString()))
        {
            para["ScanContainerName"] = m_CommonObj.ContainerScan(para["ScanContainerName"].ToString());
        }
        para.Add("OrderNo", context.Request.Form["orderNo"].ToString());
        para.Add("ProductName", context.Request.Form["productName"].ToString());
        para.Add("StepNo", context.Request.Form["stepNo"].ToString());
        para.Add("FactoryID", context.Request.Form["factoryId"].ToString());
        para.Add("PageNumber", context.Request.Form["pageNumber"].ToString());
        para.Add("PageSize", context.Request.Form["pageSize"].ToString());
        uMESProcessResult result = ypContainerBusiness.GetContainerInfoByStepAttribute(para);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 查询批次详细列表
    /// </summary>
    /// <param name="context"></param>
    public void GetContainerDetails(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult() { Result = true };
        CurrentUserInfo userInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request.Form["params"].ToString());
        foreach (DataRow item in dt.Rows)
        {
            Dictionary<string, object> para = new Dictionary<string, object>();
            para.Add("OrderNo", item["mfgOrderName"].ToString());
            para.Add("ProductId", item["productID"].ToString());
            para.Add("ContainerNo", item["ContainerNo"].ToString());
            para.Add("ContainerType", item["containerType"].ToString());
            //para.Add("WorkflowRev", item["workflowRev"].ToString());
            para.Add("WorkflowStepId", item["workflowStepId"].ToString());
            para.Add("FactoryID", item["factoryId"].ToString());
            result.DBTable.Merge(ypContainerBusiness.GetContainerDetailsInfoByStepAttr(para));
        }
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 获取批次属性
    /// </summary>
    /// <param name="context"></param>
    public void GetContainerAttr(HttpContext context)
    {
        string containerIds = context.Request.Form["containerId"].ToString();
        containerIds = "'" + containerIds + "'";
        containerIds = containerIds.Replace(",", "','");
        uMESProcessResult result = new uMESProcessResult() { Result = true };
        //获取器材检验
        string sqlattr = string.Format("SELECT ut.attributevalue,ut.userattributename FROM Userattribute ut WHERE ut.parentid in ({0}) AND ut.userattributename in ('器材检验','顾客代表') GROUP BY ut.userattributename,ut.attributevalue", containerIds);
        DataTable userAttribute_Dt = DatabaseHelper.Query_ReturnDatTable(sqlattr);
        DataRow[] qicaiDrs = userAttribute_Dt.Select("userattributename='器材检验'");
        DataRow[] gukeDrs = userAttribute_Dt.Select("userattributename='顾客代表'");
        if (qicaiDrs.Length >= 2 || gukeDrs.Length >= 2)
        {
            result.Result = false;
            result.Message = "所选批次属性信息有差异，不能同时操作";
            context.Response.Write(JsonConvert.SerializeObject(result));
            return;
        }
        DataTable containerAttrDt = new DataTable();
        containerAttrDt.Columns.Add("EquipmentCheck");//器材检验
        containerAttrDt.Columns.Add("CustomDep");//顾客代表
        DataRow containerAttrDr = containerAttrDt.NewRow();
        containerAttrDr["EquipmentCheck"] = qicaiDrs.Length == 1 ? qicaiDrs[0]["attributevalue"].ToString() : "";
        containerAttrDr["CustomDep"] = gukeDrs.Length == 1 ? gukeDrs[0]["attributevalue"].ToString() : "";
        containerAttrDt.Rows.Add(containerAttrDr);
        result.DBTable = containerAttrDt;
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    /// <summary>
    /// 获取工序属性列表
    /// </summary>
    /// <param name="context"></param>
    public void GetStepAttrList(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        uMESProcessResult result = new uMESProcessResult();
        result.Result = true;

        string workFlowID = context.Request.Form["workFlowID"].ToString();
        string containerIds = context.Request.Form["containerId"].ToString();
        string sequence = context.Request.Form["sequence"].ToString();
        string[] containerIDsObj = containerIds.Split(',');//批次
        int containerCounts = containerIDsObj.Length;//批次数
        containerIds = "'" + containerIds + "'";
        containerIds = containerIds.Replace(",", "','");
        //获取工序属性列表
        DataTable dt = ypContainerBusiness.GetWorkflowStepForSpecAttr(containerIds, workFlowID, sequence);
        if (containerIDsObj.Length >= 2) //多选批次，判断备注差异
        {
            //检查差异性 add:Wangjh 批次工序备注不一致，不能同时查询操作 
            DataRow[] tempDrs = dt.Select("containerstepattributeid is not null");
            if (tempDrs.Length > 0)
            {
                DataTable temDt = tempDrs.CopyToDataTable();
                foreach (DataRow item in tempDrs)
                {
                    int stepCount = temDt.Select(string.Format("workflowstepid='{0}' and notes = '{1}' and containerid <> '{2}'", item["workflowstepid"].ToString(), item["notes"].ToString(), item["containerid"].ToString())).Count() + 1;
                    if (stepCount % containerCounts != 0)
                    {
                        result.Result = false;
                        result.Message = "所选批次工序的备注信息有差异，不能同时操作";
                        break;
                    }
                }
            }
            if (result.Result)//只需取第一个批次
            {
                dt = ypContainerBusiness.GetWorkflowStepForSpecAttr("'" + containerIDsObj[0] + "'", workFlowID, sequence);
            }
        }
        if (result.Result)
        {
            result.DBTable = dt;
        }
        //if (tempDrs.Length > 0)
        //{
        //    DataTable attrDt = tempDrs.CopyToDataTable();
        //    DataTable stepDt = dt.DefaultView.ToTable(true, "workflowstepid");
        //    if (attrDt.Rows.Count % stepDt.Rows.Count > 0)
        //    {
        //        result.Message = "所选批次工序的备注信息有差异，不能同时操作";
        //        context.Response.Write(JsonConvert.SerializeObject(result));
        //        return;
        //    }
        //}
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 获取配套表列表
    /// </summary>
    /// <param name="context"></param>
    public void GetBomProductAttr(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult() { Result = true };
        string containerIds = context.Request.Form["containerId"].ToString();
        string bomID = context.Request.Form["bomId"].ToString();
        containerIds = "'" + containerIds + "'";
        containerIds = containerIds.Replace(",", "','");
        //获取配套表BOM
        DataTable bomDt = m_CommonObj.GetBOM(bomID);
        foreach (DataRow bdt in bomDt.Rows)
        {
            string sqlbom = string.Format("SELECT cbp.notes FROM containerbomproductattr cbp WHERE cbp.productid = '{0}' AND cbp.containerid in ({1}) GROUP BY cbp.notes", bdt["PRODUCTID"].ToString(), containerIds);
            DataTable lsDt = DatabaseHelper.Query_ReturnDatTable(sqlbom);
            if (lsDt.Rows.Count == 1)
            {
                bdt["NOTES"] = lsDt.Rows[0]["NOTES"];
            }
            else if (lsDt.Rows.Count >= 2)
            {
                result.Result = false;
                result.Message = "所选批次配置表的备注信息有差异，不能同时操作";
                break;
            }
            else
            {
                bdt["NOTES"] = "";//默认将备注置为空
            }
        }
        if (result.Result)
        {
            result.DBTable = bomDt;
        }
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 获取工序不加工列表
    /// </summary>
    /// <param name="context"></param>
    public void GetStepNoShipList(HttpContext context)
    {
        string containerIds = context.Request.Form["containerIds"].ToString();
        string workflowId = context.Request.Form["workFlowID"].ToString();
        DataSet ds = new DataSet();
        //获取工序列表
        DataTable dtStep = ypContainerBusiness.GetStepListByWorkflowId(workflowId);
        dtStep.TableName = "dtStep";
        ds.Tables.Add(dtStep);
        //获取工序不加工列表
        containerIds = "'" + containerIds + "'";
        containerIds = containerIds.Replace(",", "','");
        DataTable dtNoShip = ypContainerBusiness.GetStepNoShipList(containerIds);
        dtNoShip.TableName = "dtNoShip";
        ds.Tables.Add(dtNoShip);
        uMESProcessResult result = new uMESProcessResult() { DBDataSet = ds, Result = true };
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    /// <summary>
    /// 获取批次工序报工列表
    /// </summary>
    /// <param name="context"></param>
    public void GetStepWorkReportList(HttpContext context)
    {
        string containerIds = context.Request.Form["containerIds"].ToString();
        containerIds = "'" + containerIds + "'";
        containerIds = containerIds.Replace(",", "','");
        DataTable dtWorkRePort = ypContainerBusiness.GetStepWorkReportList(containerIds);
        uMESProcessResult result = new uMESProcessResult() { DBTable = dtWorkRePort, Result = true };
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    /// <summary>
    /// 保存批次属性
    /// </summary>
    /// <param name="context"></param>
    public void SaveContainerAttr(HttpContext context)
    {
        List<Dictionary<string, string>> para = new List<Dictionary<string, string>>();
        DataTable containerDt = JsonConvert.DeserializeObject<DataTable>(context.Request.Form["containerDt"].ToString());
        if (containerDt.Rows.Count == 0)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = "批次未选择", Result = false }));
            return;
        }
        string equipmentCheck = context.Request.Form["equipmentCheck"].ToString(); //器材检验
        string customDep = context.Request.Form["customDep"].ToString();//顾客代表
        foreach (DataRow containerdr in containerDt.Rows)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("ContainerId", containerdr["ContainerId"].ToString());
            dic.Add("EquipmentCheck", equipmentCheck);
            dic.Add("CustomDep", customDep);
            para.Add(dic);
        }
        if (ypContainerBusiness.SaveContainerAttribute(para))
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = "保存成功", Result = true }));
            return;
        }
        context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = "保存失败", Result = false }));
    }

    /// <summary>
    /// 保存批次工序属性
    /// </summary>
    /// <param name="context"></param>
    public void SaveContainerStepAttr(HttpContext context)
    {
        CurrentUserInfo userInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
        List<Dictionary<string, string>> para = new List<Dictionary<string, string>>();
        DataTable containerDt = JsonConvert.DeserializeObject<DataTable>(context.Request.Form["containerDt"].ToString());
        if (containerDt.Rows.Count == 0)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = "批次未选择", Result = false }));
            return;
        }
        string notes = "";
        bool isClear = bool.Parse(context.Request.Form["isClear"].ToString());
        if (!isClear) //如果不是清除，则添加属性
        {
            notes = context.Request.Form["notes"].ToString();
        }
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request.Form["stepDatas"].ToString());//获取已选工序
        foreach (DataRow containerdr in containerDt.Rows)
        {
            foreach (DataRow item in dt.Rows)
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("ContainerId", containerdr["ContainerId"].ToString());
                dic.Add("WorkFlowStepId", item["WORKFLOWSTEPID"].ToString());
                if (containerDt.Rows.Count == 1)
                {
                    dic.Add("ContainerStepAttributeId", item["CONTAINERSTEPATTRIBUTEID"].ToString());
                }
                dic.Add("isClear", isClear ? "1" : "0");
                dic.Add("CreateEmployeeId", userInfo.EmployeeID.ToString());
                dic.Add("Notes", notes);
                dic.Add("OldNotes", item["Notes"].ToString());
                para.Add(dic);
            }
        }

        if (ypContainerBusiness.SaveContainerStepAttribute(para))
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = isClear ? "清除成功" : "保存成功", Result = true }));
            return;
        }
        context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = isClear ? "清除失败" : "保存失败", Result = false }));
    }
    /// <summary>
    /// 保存配置表属性
    /// </summary>
    /// <param name="context"></param>
    public void SaveAccessoriesAttr(HttpContext context)
    {
        CurrentUserInfo userInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
        List<Dictionary<string, string>> para = new List<Dictionary<string, string>>();
        DataTable containerDt = JsonConvert.DeserializeObject<DataTable>(context.Request.Form["containerDt"].ToString());
        if (containerDt.Rows.Count == 0)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = "批次未选择", Result = false }));
            return;
        }
        bool isClear = bool.Parse(context.Request.Form["isClear"].ToString());
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request.Form["bomProducts"].ToString());
        string notes = "";
        if (!isClear) //如果不是清除，则添加属性
        {
            notes = context.Request.Form["notes"].ToString();
        }
        foreach (DataRow containerdr in containerDt.Rows)
        {
            foreach (DataRow item in dt.Rows)
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("ContainerId", containerdr["ContainerId"].ToString());
                dic.Add("ProductId", item["PRODUCTID"].ToString());
                dic.Add("isClear", isClear ? "1" : "0");
                if (!isClear)//如果不是清除，则添加
                {
                    string sqlbom = string.Format("SELECT * FROM containerbomproductattr cbp WHERE cbp.productid = '{0}' AND cbp.containerid = '{1}'", item["PRODUCTID"].ToString(), containerdr["ContainerId"].ToString());
                    DataTable lsDt = DatabaseHelper.Query_ReturnDatTable(sqlbom);
                    dic.Add("IsBomProduct", lsDt.Rows.Count == 0 ? "0" : "1");//是否存在
                    dic.Add("Notes", notes);
                    dic.Add("CreateEmployeeId", userInfo.EmployeeID.ToString());
                }
                para.Add(dic);
            }
        }
        if (ypContainerBusiness.SaveContainerBomProductAttribute(para))
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = isClear ? "清除成功" : "保存成功", Result = true }));
            return;
        }
        context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = isClear ? "清除失败" : "保存失败", Result = false }));
    }
    /// <summary>
    /// 添加工序不加工数据
    /// </summary>
    /// <param name="context"></param>
    public void AddStepNoShipDatas(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        CurrentUserInfo userInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
        ServerHostInfo host = new ServerHostInfo();
        host.UserName = userInfo.APIUserName;
        host.Password = userInfo.APIPassword;
        DataTable levelAttributes = m_CommonObj.GetFatoryAttributes(userInfo.FactoryID);
        List<Dictionary<string, string>> para = new List<Dictionary<string, string>>();
        DataTable containerDt = JsonConvert.DeserializeObject<DataTable>(context.Request.Form["containerDt"].ToString());
        if (containerDt.Rows.Count == 0)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = "批次未选择", Result = false }));
            return;
        }
        DataTable dtMain = new DataTable();
        dtMain.Columns.Add("ContainerId");
        dtMain.Columns.Add("ContainerName");
        dtMain.Columns.Add("WorkflowStepId");
        dtMain.Columns.Add("WorkflowStepName");
        dtMain.Columns.Add("CreateEmployeeId");
        DataTable stepDt = JsonConvert.DeserializeObject<DataTable>(context.Request.Form["stepDt"].ToString());
        foreach (DataRow containerdr in containerDt.Rows)
        {
            foreach (DataRow item in stepDt.Rows)
            {
                DataRow dr = dtMain.NewRow();
                dr["ContainerId"] = containerdr["ContainerId"].ToString();
                dr["ContainerName"] = containerdr["ContainerName"].ToString();
                dr["WorkflowStepId"] = item["WORKFLOWSTEPID"].ToString();
                dr["WorkflowStepName"] = item["WORKFLOWSTEPNAME"].ToString();
                dr["CreateEmployeeId"] = userInfo.EmployeeID.ToString();
                dtMain.Rows.Add(dr);
            }
        }

        result = ypContainerBusiness.IsValidStepNoShip(dtMain);//验证是否有效
        if (!result.Result)
        {
            context.Response.Write(JsonConvert.SerializeObject(result));
            return;
        }
        result = apsPlanDownSend.SaveApsPlanDatas(new Dictionary<string, string> { { "type", "ReCall" }, { "EmployeeID",userInfo.EmployeeID } }, dtMain);//回收排产数据
        if (!result.Result)
        {
            context.Response.Write(JsonConvert.SerializeObject(result));
            return;
        }
        result.Result = ypContainerBusiness.CreateStepNoShip(dtMain);
        if (!result.Result) 
        {
            result.Result = false; result.Message = "排产下发回收成功，保存不加工失败";
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
        //添加不加工数据成功后，开始移动工序
        string msg = "";
        foreach (DataRow item in containerDt.Rows)
        {
            //获取当前批次不加工数据
            DataTable dtNoStep = ypContainerBusiness.GetStepNoShipList("'" + item["ContainerId"].ToString() + "'");
            while (true) //循环判断一个批次的所有连续不加工工序
            {
                //获取批次的数据，验证批次的当前工序是否存在不加工
                DataTable dt = m_CommonObj.GetCurrentInfoByContainerID(item["ContainerId"].ToString());
                DataRow drContainer = dt.Rows[0];
                //获取工艺下的所有转工，外委，检验工序
                DataTable dtOtherStep = m_CommonObj.GetWorkFlowStepsWithSpceControlCodeAndCheckStep(drContainer["WorkflowID"].ToString(), drContainer["OriginalFactoryID"].ToString());
                //获取当前工序不加工数据
                DataRow[] drNo = dtNoStep.Select("workflowstepid='" + drContainer["WorkflowstepID"].ToString() + "'");
                //获取当前工序是否转工、外委、检验工序
                DataRow[] drOther = dtOtherStep.Select("workflowstepid='" + drContainer["WorkflowstepID"].ToString() + "'");
                //如果当前序存在不加工，且不是（转工、外委、检验序）的情况，则移动
                if (drNo.Length > 0 && drOther.Length == 0)
                {
                    checkBusiness.MoveNextStep(new Dictionary<String, String>() {{"ContainerID", drContainer["ContainerID"].ToString()},
                                                                                    {"WorkflowStepID", drContainer["WorkFlowStepID"].ToString()},
                                                                                     {"WorkflowID", drContainer["WorkflowID"].ToString()},
                                                                                    {"Sequence", drNo[0]["Sequence"].ToString()},
                                                                                    {"ContainerName", drContainer["ContainerName"].ToString()}
                                                                                   }, host, int.Parse(drContainer["Qty"].ToString()), levelAttributes, userInfo, ref msg);
                }
                else
                {
                    break;
                }
            }
        }
        result.Result = true; result.Message = "保存成功";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    /// <summary>
    /// 移除工序不加工数据
    /// </summary>
    /// <param name="context"></param>
    public void RemoveStepNoShipDatas(HttpContext context)
    {
        List<Dictionary<string, string>> para = new List<Dictionary<string, string>>();
        DataTable stepNoShipDt = JsonConvert.DeserializeObject<DataTable>(context.Request.Form["stepNoShipDt"].ToString());
        if (stepNoShipDt.Rows.Count == 0)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = "未选择工序不加工数据", Result = false }));
            return;
        }
        if (ypContainerBusiness.RemoveStepNoShip(stepNoShipDt))
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = "移除成功", Result = true }));
            return;
        }
        context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Message = "移除失败", Result = false }));
    }
    /// <summary>
    /// 保存工序报工备注信息
    /// </summary>
    /// <param name="context"></param>
    public void SaveStepWorkReportData(HttpContext context)
    {
        string notes = context.Request.Form["notes"].ToString().Trim();
        string workreportIDs = context.Request.Form["workreportIDs"].ToString();
        workreportIDs = "'" + workreportIDs + "'";
        workreportIDs = workreportIDs.Replace(",", "','");
        uMESProcessResult result = new uMESProcessResult() { Result = true };
        Dictionary<string, string> para = new Dictionary<string, string>();
        para.Add("notes", notes);
        para.Add("workreportIDs", workreportIDs);
        result.Result = ypContainerBusiness.SaveStepWorkReportNotesData(para);
        result.Message = result.Result ? "保存成功" : "保存失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
}